using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Threading;
using Test;
using System.Text.RegularExpressions;
using System.Data;
namespace EvLog {
	static class EvLog {
		public static DAL local=new DAL();
		static void MainX() {


			//double i=Math.Ceiling(((double)4/((double)3-(double)3))*100d);

			List<string> MachineNames=new List<string>();
			//MachineNames.AddRange(Mixer.CombineEx("pon-wpct{0}","51-75"));
			
			//PWMS
			//MachineNames.AddRange(Mixer.CombineEx("pon-wpct{0:00}","1-30"));
			MachineNames.AddRange(Mixer.CombineEx("pon-wprf0{0}-cl0{1}","3,6","1,2"));

			//MachineNames.AddRange(Mixer.Combine("pon-wpas10-cl0{0}","1,2"));
			//MachineNames.AddRange(Mixer.CombineEx("pon-wprf{0:00}-cl0{1}","1-3,5-6","1,2"));
			//MachineNames.AddRange(Mixer.Combine("pon-wprf0{0}-cl0{1}","1","1,2"));
			//MachineNames.AddRange(Mixer.Combine("pon-wprf0{0}-cl0{1}","6","1,2"));
			//MachineNames.AddRange(Mixer.CombineEx("pon-wpwb{0:00}-cl{1:00}","2","1,2"));
			//MachineNames.AddRange(Mixer.CombineEx("pon-wpas10-cl{0:00}","1,2"));
			//MachineNames.AddRange(Mixer.CombineEx("pon-wprf{0:00}-cl{1:00}","1","1,2"));
			//MachineNames.AddRange(Mixer.Combine("pon-wpas10-cl0{0}","1,2"));
			//MachineNames.Add("pon-wpsq21-db03");
			//MachineNames.AddRange(Mixer.Combine("pon-wpsq{0}-db","01,06,07,08,09,12,13,16"));

			//MachineNames.AddRange(Mixer.Combine("pon-wpmq01-cl0{0}","1,2"));
			//MachineNames.AddRange(Mixer.CombineEx("pon-wpsq21-cl{0:00}","1-6"));
			//MachineNames.Add("PON-WPRF04-CL01");
			//MachineNames.Add("PON-WPRF04-CL02");
			//MachineNames.Add("PON-WPSQ09-CL01");
			//MachineNames.Add("PON-WPSQ09-CL02");
			//MachineNames.Add("PON-WPSQ03-CL01");
			//MachineNames.Add("pon-wpsq21-db03");
			//MachineNames.Add("pon-wpsq08-db");
			local.NonQry("truncate table elog");
			List<Thread> thLst=new List<Thread>();
			foreach(string machine in MachineNames)
				thLst.Add(new Thread(new LogReader(machine).Read));
			Stopwatch sw=Stopwatch.StartNew();
			foreach(Thread th in thLst)
				th.Start();
			foreach(Thread th in thLst)
				th.Join();
			Console.WriteLine("THE END");
			Console.WriteLine(sw.Elapsed);
		}
	}
	class LogReader {
		string format="insert into elog(cat,eType,machine,src,eTime,usr,msg,log,data)values('{0}','{1}','{2}','{3}',convert(datetime,'{4}',120),'{5}','{6}','{7}','{8}')";
		string _machine;
		public LogReader(string machine) { _machine=machine; }
		public void Read() {
			EventLog[] logs=new EventLog[0];
			try
			{
				logs=EventLog.GetEventLogs(_machine);
			}
			catch(Exception ex) { Console.WriteLine(ex.Message+" - "+_machine); }
			foreach(EventLog log in logs)
				if(log.Log.ToLower()=="application")
					//if(string.Compare("application",log.Log,true)==0)
					//if(log.Log.ToLower()!="security")
					for(int i=log.Entries.Count-1;i>=0;i--)
					{
						EventLogEntry ent=log.Entries[i];

						//if(ent.TimeWritten.Date.AddDays(1)<DateTime.Now.Date)
						if(ent.TimeWritten<DateTime.Now.AddHours(-2))
						//if(ent.TimeWritten<DateTime.Now.AddDays(-1))
							break;


						//if(ent.TimeWritten>new DateTime(2013,9,29,0,50,0))//to
						//  continue;
						//if(ent.TimeWritten<new DateTime(2013,9,29,0,0,0))//from
						//  break;

						string data="";
						//if(ent.Source.ToLower().Contains("next"))
						//if(ent.Source=="Next.Whs.WCS.Core")
						//if(ent.Message.Contains("failed for user"))
						//string user="";
						//if(ent.EntryType==EventLogEntryType.Error)
						//if(ent.Message.Contains("CheckAndAdd failed for address"))
						//if(ent.Message.Contains("NRETIDTO"))
						//if(ent.Message.Contains("TargetedConfirmedPutaway"))
						//if(ent.Message.Contains("OutOfMemoryException"))
						//if(ent.Message.Contains("Method not found"))
						//if(ent.Message.Contains("Method not found"))
						//if(ent.Message.ToLower().Contains("login failed for user"))
						//if(ent.Message.Contains("DirPackingSvr_MI"))
						//if(ent.Message.Contains("donwh"))
						//if(ent.Message.Contains("Unable to configure Label")||ent.Message.Contains("within"))
						{
							//data=Regex.Match(ent.Message,"User: NEXT-PLC\\\\(.+?)\r\n",RegexOptions.Singleline).Groups[1].Value;

							//Console.WriteLine(ent.Message);
							//string[] arr=ent.Message.Split(',');
							//data=arr[4].Replace("'","").Trim();
							EvLog.local.NonQry(format,ent.Category,ent.EntryType,ent.MachineName,ent.Source,ent.TimeGenerated.ToString("yyyy-MM-dd HH:mm:ss"),ent.UserName,ent.Message.Replace("'","''"),log.Log.ToLower(),data);
						}

					}
			Console.WriteLine(_machine);
		}
	}
}
/*
drop table elog
create table elog(
id int identity,
cat varchar(255),
eType varchar(25),
machine varchar(50),
log varchar(30),
src varchar(255),
eTime datetime,
usr varchar(50),
msg varchar(max),
constraint pk_elog_id primary key(id)
)
create index ix_elog_eTime on elog(eTime)
*/
